package me.zhengjie.server.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.server.domain.ServerInfo;
import me.zhengjie.server.service.ServerInfoService;
import me.zhengjie.server.service.dto.ServerInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author yhg
* @date 2019-11-25
*/
@Api(tags = "ServerInfo管理")
@RestController
@RequestMapping("/api/serverInfo")
public class ServerInfoController {

    private final ServerInfoService serverInfoService;

    public ServerInfoController(ServerInfoService serverInfoService) {
        this.serverInfoService = serverInfoService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('serverInfo:list')")
    public void download(HttpServletResponse response, ServerInfoQueryCriteria criteria) throws IOException {
        serverInfoService.download(serverInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询ServerInfo")
    @ApiOperation("查询ServerInfo")
    @PreAuthorize("@el.check('serverInfo:list')")
    public ResponseEntity getServerInfos(ServerInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(serverInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增ServerInfo")
    @ApiOperation("新增ServerInfo")
    @PreAuthorize("@el.check('serverInfo:add')")
    public ResponseEntity create(@Validated @RequestBody ServerInfo resources){
        return new ResponseEntity<>(serverInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改ServerInfo")
    @ApiOperation("修改ServerInfo")
    @PreAuthorize("@el.check('serverInfo:edit')")
    public ResponseEntity update(@Validated @RequestBody ServerInfo resources){
        serverInfoService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping(value = "/{id}")
    @Log("删除ServerInfo")
    @ApiOperation("删除ServerInfo")
    @PreAuthorize("@el.check('serverInfo:del')")
    public ResponseEntity delete(@PathVariable Long id){
        serverInfoService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}